# from recommendation import critics
from math import sqrt
def clus_pearson(vec1,vec2):

    n=len(vec1)

    sum1 = sum(vec1)
    sum2 = sum(vec2)

    psum1 = sum([pow(i,2) for i in vec1])
    psum2 = sum([pow(i,2) for i in vec2])

    msum = sum([vec1[i]*vec2[i] for i in range(len(vec1))])

    num = msum-(sum1*sum2)/n
    den = sqrt((psum1-(pow(sum1,2)/n))*(psum2-(pow(sum2,2)/n)))

    if den==0:
        return 0
    return 1.0-num/den

def sim_pearson(pref,person1,person2):
    si={}
    for i in pref[person1]:
        if i in pref[person2]:
            si[i]=1

    n=len(si)
    if n==0: return 0

    sum1 = sum([pref[person1][i] for i in si])
    sum2 = sum([pref[person2][i] for i in si])

    psum1 = sum([pow(pref[person1][i],2) for i in si])
    psum2 = sum([pow(pref[person2][i],2) for i in si])

    msum = sum([pref[person1][i]*pref[person2][i] for i in si])

    num = msum-(sum1*sum2/n)
    den = sqrt((psum1-pow(sum1,2)/n) * (psum2-pow(sum2,2)/n))
    if den ==0 : return 0

    r=num/den

    return r

# print sim_pearson(critics,'Lisa Rose','Gene Seymour')